package com.bw.module_inspection.viewmodel

import androidx.lifecycle.MutableLiveData
import com.bw.lib_common.coroutine.CoroutineHelper
import com.bw.lib_common.vm.BaseViewModel2
import com.bw.module_inspection.model.AddInspectionEntity
import com.bw.module_inspection.model.InspectionEntity
import com.bw.module_inspection.repo.InspectionRepo
import kotlinx.coroutines.launch
/**
 * 智慧园区-07-巡检管理V1.1 史建军20250512
 */
class InspectionViewModel:BaseViewModel2<InspectionRepo>(){
    val inspectionSuccess by lazy { MutableLiveData<List<InspectionEntity>>() }
    val addInspectionSuccess by lazy { MutableLiveData<AddInspectionEntity>() }
    val inspectionDetailSuccess by lazy { MutableLiveData<InspectionEntity>() }
    val inspectionAgentSuccess by lazy { MutableLiveData<List<InspectionEntity>>() }
    fun getInspectionList(){
        CoroutineHelper.mViewModelScope.launch (CoroutineHelper.mDispatcher){
            val result=mRepository.getInspectionList()
            if (result.code==200){
                inspectionSuccess.postValue(result.data)
            }else{
                mErrorLiveData.postValue(result.message)
            }
        }
    }
    fun addInspection(addInspection: AddInspectionEntity){
        CoroutineHelper.mViewModelScope.launch(CoroutineHelper.mDispatcher){
            val result=mRepository.addInspection(addInspection)
            if (result.code==200){
                addInspectionSuccess.postValue(result.data)
            }else{
                mErrorLiveData.postValue(result.message)

            }
        }
    }
    fun getInspectionDetail(id:Int){
        CoroutineHelper.mViewModelScope.launch  (CoroutineHelper.mDispatcher){
            val result=mRepository.getInspectionDetail(id)
            if (result.code==200){
                inspectionDetailSuccess.postValue(result.data)
            }else{
                mErrorLiveData.postValue(result.message)
            }
        }
    }
    fun getInspectionAgent(location:String,name:String){
        CoroutineHelper.mViewModelScope.launch  (CoroutineHelper.mDispatcher){
            val result=mRepository.getInspectionAgent(location,name)
            if (result.code==200){
                inspectionAgentSuccess.postValue(result.data)
            }else{
                mErrorLiveData.postValue(result.message)
            }
        }
    }






    override fun createRepository(): InspectionRepo = InspectionRepo()
}